package com.thread;

import java.util.LinkedList;

public class JUC_taotao_interview2_synchronized_24 {
    static private int MAX = 10;
    final private LinkedList<Object> list = new LinkedList<>();
    private int count = 0;
    //生产者
    private synchronized void put(Object obj){
        while (list.size() == MAX){//为什么不用if?
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        ++count;
        list.add(new Object());
        System.out.println(Thread.currentThread().getName() + " product ... ");
        this.notifyAll();
    }
    //消费者
    private synchronized Object get(){
        while(list.size() == 0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        --count;
        Object obj = list.removeFirst();
        System.out.println(Thread.currentThread().getName() + " revome consumer ... ");
        this.notifyAll();
        return obj;
    }

    public static void main(String[] args) {
        JUC_taotao_interview2_synchronized_24 t = new JUC_taotao_interview2_synchronized_24();
        for(int i=0;i<2;i++){
            new Thread(()->{
                for(int j=0;j<100;++j){
                    t.put(Thread.currentThread().getName()+"put");
                }

            },"p"+i).start();
        }
        for(int i=0;i<10;i++){
            new Thread(()->{
                t.get();
            },"c"+i).start();
        }
    }
}
